package org.jie.guider.java.base.junit.extend;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Parameter;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

/**
 * @author LiJie
 */
public class RandomParameterExtension implements ParameterResolver {


    @Override
    public boolean supportsParameter
        (ParameterContext parameterContext, ExtensionContext extensionContext)
        throws ParameterResolutionException {

        return parameterContext.isAnnotated(Random.class);
    }

    @Override
    public Object resolveParameter
        (ParameterContext parameterContext, ExtensionContext extensionContext)
        throws ParameterResolutionException {

        return getRandomValue(parameterContext.getParameter(), extensionContext);
    }

    private Object getRandomValue
        (Parameter parameter, ExtensionContext extensionContext) {

        Class<?> type = parameter.getType();
        java.util.Random random = extensionContext.getRoot()
            .getStore(ExtensionContext.Namespace.GLOBAL)
            .getOrComputeIfAbsent(java.util.Random.class);

        if (int.class.equals(type)) {
            return random.nextInt();
        }
        if (double.class.equals(type)) {
            return random.nextDouble();
        }
        throw new ParameterResolutionException
            ("No random generator implement for " + type);
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.PARAMETER)
    public @interface Random {
    }
}
